/*
 * DeleteAnchorPointAction.java
 * 
 * Created by demory on Oct 16, 2009, 8:47:59 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.apps.cart.actions;

import java.util.HashSet;
import java.util.Set;
import org.fpdev.apps.cart.Cartographer;
import org.fpdev.apps.cart.Editor;
import org.fpdev.apps.cart.network.AnchorPoint;
import org.fpdev.apps.cart.network.Corridor;

/**
 *
 * @author demory
 */
public class DeleteAnchorPointAction extends EditorBasedAction {

  private AnchorPoint point_;
  private double x_, y_;

  private Set<DeleteCorridorAction> delCorrActions_;

  public DeleteAnchorPointAction(Editor ed, double x, double y) {
    super(ed);
    x_ = x;
    y_ = y;
  }

  public boolean doAction(Cartographer cart) {
    point_ = ed_.getDocument().getNetwork().getPointAtXY(x_, y_, ed_.getFrame().getCanvas().getClickToleranceW());
    delCorrActions_ = new HashSet<DeleteCorridorAction>();
    if(point_ != null) {
      for(Corridor corr : ed_.getDocument().getNetwork().incidentCorridors(point_))
        delCorrActions_.add(new DeleteCorridorAction(ed_, corr));
      for(DeleteCorridorAction dca : delCorrActions_) dca.doAction(cart);
      ed_.getDocument().getNetwork().deleteAnchorPoint(point_);
      return true;
    }
    return false;
  }

  public boolean undoAction(Cartographer cart) {
    if(point_ != null) {
      ed_.getDocument().getNetwork().addAnchorPoint(point_);
      for(DeleteCorridorAction delCorrAction : delCorrActions_)
        delCorrAction.undoAction(cart);
      return true;
    }
    return false;
  }

  public String getName() {
    return "Delete Anchor Point";
  }

}
